package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import android.icu.text.CaseMap.Title
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import com.grechur.wanandroidcompose.bean.article.Article
import com.grechur.wanandroidcompose.http.NetworkRequest
import com.grechur.wanandroidcompose.http.service.CollectService

class CollectViewModel(application: Application) : AndroidViewModel(application) {
    var collectArticles by mutableStateOf(Result.success(listOf<Article>()))
        private set

    var currentIndex by mutableStateOf(-1)
        private set

    var isCollect by mutableStateOf(false)
        private set

    var isUpdate by mutableStateOf(false)
        private set

    private val service = NetworkRequest.getService(getApplication(),CollectService::class.java)

    suspend fun getCollectArticles(index: Int) {
        try {
            if (index <= currentIndex) {
                return
            }
            val collectArticlesNet = service.getCollectArticles(index)
            collectArticlesNet.takeIf { it != null }?.let { articlesBean ->
                if (articlesBean.errorCode == 0) {

                } else {

                }
            }
        } catch (e:Exception) {

        }
    }

    suspend fun collectArticle(cid:Int?) {
        val result = try {
            val collect = service.collectArticle(cid)
            collect.takeIf { it != null }?.let { base ->
                base.errorCode == 0
            }?:false
        } catch (e:Exception) {
            false
        }

        isCollect = result
    }

    suspend fun collectOtherArticle(title: String?, author: String?, link: String?) {
        val result = try {
            val collect = service.collectOtherArticle(title, author, link)
            collect.takeIf { it != null }?.let { base ->
                base.errorCode == 0
            }?:false
        } catch (e:Exception) {
            false
        }

        isCollect = result
    }


    suspend fun upDateArticle(cid: Int,title: String, author: String, link: String) {
        val result = try {
            val collect = service.updateCollect(cid,title, author, link)
            collect.takeIf { it != null }?.let { base ->
                base.errorCode == 0
            }?:false
        } catch (e:Exception) {
            false
        }

        isUpdate = result
    }


    suspend fun unCollectArticle(id:Int?) {
        val result = try {
            val collect = service.unCollectArticle(id)
            collect.takeIf { it != null }?.let { base ->
                base.errorCode == 0
            }?:false
        } catch (e:Exception) {
            false
        }

        isCollect = !result
    }

    suspend fun unCollectList(id:Int?,originId:Int?) {
        val result = try {
            val collect = service.unCollectList(id,originId)
            collect.takeIf { it != null }?.let { base ->
                base.errorCode == 0
            }?:false
        } catch (e:Exception) {
            false
        }

        isCollect = !result
    }
}